package com.mall.dubbo.api;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mall.dubbo.mapper.UmsAdminMapper;
import com.mall.dubbo.mapper.UmsAdminRoleRelationMapper;
import com.mall.model.domain.UmsAdmin;
import com.mall.model.domain.UmsAdminRoleRelation;
import com.mall.model.domain.UmsRole;
import org.apache.commons.lang.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

/**
 * @author xyiaao
 * @description 针对表【ums_admin(后台用户表)】的数据库操作Api实现
 * @createDate 2022-03-29 19:46:09
 */
@DubboService
public class UmsAdminApiImpl extends ServiceImpl<UmsAdminMapper, UmsAdmin>
        implements UmsAdminApi {

    @Autowired
    private UmsAdminMapper umsAdminMapper;

    @Autowired
    private UmsAdminRoleRelationMapper umsAdminRoleRelationMapper;

    @Override
    public UmsAdmin findUmsAdmin(String username, String password) {

        LambdaQueryWrapper<UmsAdmin> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(UmsAdmin::getId, UmsAdmin::getPassword, UmsAdmin::getStatus);
        // queryWrapper.eq(UmsAdmin::getUsername, username).eq(UmsAdmin::getPassword, password);
        queryWrapper.eq(UmsAdmin::getUsername, username);
        // queryWrapper.eq(UmsAdmin::getUsername, username)
        //         .eq(UmsAdmin::getPassword, "$2a$10$.E1FokumK5GIXWgKlg.Hc.i/0/2.qdAwYFL1zc5QHdyzpXOr38RZO");
        return umsAdminMapper.selectOne(queryWrapper);
    }

    @Override
    public UmsAdmin findUmsAdminById(Long id) {

        return umsAdminMapper.selectById(id);
    }

    @Override
    public boolean delete(Long id) {

        LambdaQueryWrapper<UmsAdminRoleRelation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UmsAdminRoleRelation::getAdminId, id);
        umsAdminRoleRelationMapper.delete(queryWrapper);
        return removeById(id);
    }

    @Override
    public Page<UmsAdmin> listPage(String keyword, Integer pageNum, Integer pageSize) {

        Page<UmsAdmin> pageInfo = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<UmsAdmin> queryWrapper = new LambdaQueryWrapper<>();
        // 用户名类似
        queryWrapper.like(StringUtils.isNotEmpty(keyword), UmsAdmin::getUsername, keyword)
                // 昵称类似
                .or().like(StringUtils.isNotEmpty(keyword), UmsAdmin::getNickName, keyword);
        return umsAdminMapper.selectPage(pageInfo, queryWrapper);
    }

    @Override
    public UmsAdmin register(UmsAdmin umsAdmin) {

        umsAdmin.setCreateTime(new Date());
        umsAdmin.setStatus(1);
        umsAdminMapper.insert(umsAdmin);
        return umsAdmin;
    }

    @Override
    public void roleUpdate(Integer adminId, List<Integer> roleIds) {

        LambdaQueryWrapper<UmsAdminRoleRelation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UmsAdminRoleRelation::getAdminId, adminId);
        umsAdminRoleRelationMapper.delete(queryWrapper);
        for (Integer roleId : roleIds) {
            UmsAdminRoleRelation relation = new UmsAdminRoleRelation();
            relation.setAdminId(Long.valueOf(adminId));
            relation.setRoleId(Long.valueOf(roleId));
            umsAdminRoleRelationMapper.insert(relation);
        }
    }

    @Override
    public UmsRole findRoleByAdminId(Integer adminId) {

        return umsAdminMapper.findRoleByAdminId(adminId);
    }

    @Override
    public UmsAdmin findUmsAdminByUsername(String username) {

        LambdaQueryWrapper<UmsAdmin> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UmsAdmin::getUsername, username);
        return umsAdminMapper.selectOne(queryWrapper);
    }

}




